import request from "../../utils/http";
import { anyObj, enter, showConfirm, wxToast } from "../../utils/util";

interface AddressAndFamilyMember {
	addressesId: number;
	tag: string;
	contacts: string;
	phone: string;
	detailAddress: string;
	outletId: number;
	familyMembersVos: FamilyMembersVos[]; 
}
  
interface FamilyMembersVos {
	id: number;
	name: string;
	gender: number;
	age: number;
	healthInfo: string;
	isDefault: 0 | 1; 
}  

// pages/reservationPro/reservationPro.ts
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		query: <anyObj>{},
		addressAndFamilyMember: <null | AddressAndFamilyMember>null,

		detailAddress: '',
		contacts: '',
		street: '',
		phone: '',
		addressId: 0,
		outletId: 0,

		familyMemberId: 0,
	},

	onChange(e: WechatMiniprogram.CustomEvent) {
		this.setData({
			[e.currentTarget.dataset.key]: e.detail.value
		});
	},

	/**
	 * 点击编辑地址
	 */
	editAddress() {
		enter({
			pageName: 'saveAddress',
			query: {
				addressId: this.data.addressId
			}
		});
	},

	/**
	 * 点击选择新地址
	 */
	chooseAddress() {
		enter({
			pageName: 'chooseAddress',
			query: {
				addressId: this.data.addressId
			}
		});
	},

	/**
	 * 点击编辑/新增家庭成员
	 */
	saveFamilyMembers(e: WechatMiniprogram.CustomEvent) {
		enter({
			pageName: 'saveFamilyMembers',
			query: {
				familyMemberId: e.currentTarget.dataset.id
			}
		});
	},

	async getAddressAndFamilyMember() {
		try {
			const addressAndFamilyMember: AddressAndFamilyMember = await request({
				url: '/api/customers/reservation/getAddressAndFamilyMember',
				method: 'GET'
			});
			if (!addressAndFamilyMember.familyMembersVos) {
				addressAndFamilyMember.familyMembersVos = [];
			} else {
				const item = addressAndFamilyMember.familyMembersVos.find(item => item.isDefault == 1);
				if (item) {
					this.setData({
						familyMemberId: item.id
					});
				}
			}
			this.setData({
				addressAndFamilyMember
			});
			if (!this.data.addressId) {
				this.setData({
					detailAddress: addressAndFamilyMember.detailAddress,
					contacts: addressAndFamilyMember.contacts,
					phone: addressAndFamilyMember.phone,
					addressId: addressAndFamilyMember.addressesId,
					outletId: addressAndFamilyMember.outletId,
				});
			}
		} catch (error) {
			
		}
	},

	/**
	 * 点击提交
	 */
	async submit() {
		try {
			await showConfirm({content: '已确定地址和服务对象?'});
			await request({
				url: '/api/customers/reservation/selectAddressAndMember',
				data: {
					addressId: this.data.addressId,
					familyMemberId: this.data.familyMemberId,
					orderId: this.data.query.orderId
				},
				errToast: true,
				showLoading: {title: '正在成功...'},
				contentTypeForm: true
			});
			wxToast({icon: 'success', title: '提交成功'});
			enter({
				url: `/pages/chooseScheduleDate/chooseScheduleDate`,
				query: {
					outletId: this.data.outletId,
					orderId: this.data.query.orderId,
					serviceId: this.data.query.serviceId
				}
			});
		} catch (error) {
			
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(query = {}) {
		this.setData({query});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {
		this.getAddressAndFamilyMember();
		const CHOOSE_ADDRESS = wx.getStorageSync("CHOOSE_ADDRESS");
		if (CHOOSE_ADDRESS) {
			const data = JSON.parse(CHOOSE_ADDRESS);
			this.setData({
				detailAddress: data.detailAddress,
				contacts: data.contacts,
				phone: data.phone,
				addressId: data.id,
				outletId: data.outletId,
				street: data.street
			});
			wx.removeStorage({key: "CHOOSE_ADDRESS"});
		}

		const RESERVATION_USER_ID = wx.getStorageSync("RESERVATION_USER_ID");
		if (RESERVATION_USER_ID) {
			this.setData({
				familyMemberId: RESERVATION_USER_ID
			});
			wx.removeStorage({key: "RESERVATION_USER_ID"});
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {

	}
})